home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVWDDB.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  18KB  |  615 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevwddb.c */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript,
  22.  * using device-dependent bitmap.
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  26.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  27.  */
  28. #include "gdevmswn.h"
  29.  
  30. /* Make sure we cast to the correct structure type. */
  31. typedef struct gx_device_win_ddb_s gx_device_win_ddb;
  32. #undef wdev
  33. #define wdev ((gx_device_win_ddb *)dev)
  34.  
  35. /* Forward references */
  36. private void near win_addtool(P2(gx_device_win_ddb *, int));
  37. private void near win_maketools(P2(gx_device_win_ddb *, HDC));
  38. private void near win_destroytools(P1(gx_device_win_ddb *));
  39.  
  40. /* Device procedures */
  41.  
  42. /* See gxdevice.h for the definitions of the procedures. */
  43. private dev_proc_open_device(win_ddb_open);
  44. private dev_proc_close_device(win_ddb_close);
  45. private dev_proc_map_rgb_color(win_ddb_map_rgb_color);
  46. private dev_proc_fill_rectangle(win_ddb_fill_rectangle);
  47. private dev_proc_tile_rectangle(win_ddb_tile_rectangle);
  48. private dev_proc_copy_mono(win_ddb_copy_mono);
  49. private dev_proc_copy_color(win_ddb_copy_color);
  50. private dev_proc_draw_line(win_ddb_draw_line);
  51. /* Windows-specific procedures */
  52. private win_proc_copy_to_clipboard(win_ddb_copy_to_clipboard);
  53. private win_proc_repaint(win_ddb_repaint);
  54. private win_proc_alloc_bitmap(win_ddb_alloc_bitmap);
  55. private win_proc_free_bitmap(win_ddb_free_bitmap);
  56.  
  57. /* The device descriptor */
  58. struct gx_device_win_ddb_s {
  59.     gx_device_common;
  60.     gx_device_win_common;
  61.  
  62.     /* Handles */
  63.  
  64.     HBITMAP FAR hbitmap;
  65.     HDC FAR hdcbit;
  66.     HPEN hpen, *hpens;
  67.     uint hpensize;
  68.     HBRUSH hbrush, *hbrushs;
  69.     uint hbrushsize;
  70. #define select_brush(color)\
  71.   if (wdev->hbrush != wdev->hbrushs[color])\
  72.    {    wdev->hbrush = wdev->hbrushs[color];\
  73.     SelectObject(wdev->hdcbit,wdev->hbrush);\
  74.    }
  75.     HPALETTE hpalette;
  76.     LPLOGPALETTE lpalette;
  77.  
  78.     /* A staging bitmap for copy_mono. */
  79.     /* We want one big enough to handle the standard 16x16 halftone; */
  80.     /* this is also big enough for ordinary-size characters. */
  81.  
  82. #define bmWidthBytes 4        /* must be even */
  83. #define bmWidthBits (bmWidthBytes * 8)
  84. #define bmHeight 32
  85.     HBITMAP FAR hbmmono;
  86.     HDC FAR hdcmono;
  87.     gx_bitmap_id bm_id;
  88. };
  89. private gx_device_procs win_ddb_procs = {
  90.     win_ddb_open,
  91.     gx_default_get_initial_matrix,
  92.     win_sync_output,
  93.     win_output_page,
  94.     win_ddb_close,
  95.     win_ddb_map_rgb_color,
  96.     win_map_color_rgb,
  97.     win_ddb_fill_rectangle,
  98.     win_ddb_tile_rectangle,
  99.     win_ddb_copy_mono,
  100.     win_ddb_copy_color,
  101.     win_ddb_draw_line,
  102.     gx_default_get_bits,
  103.     gx_default_get_props,
  104.     win_put_props,
  105.     gx_default_map_cmyk_color,
  106.     win_get_xfont_procs
  107. };
  108. gx_device_win_ddb gs_mswin_device = {
  109.     sizeof(gx_device_win_ddb),
  110.     &win_ddb_procs,
  111.     "mswin",
  112.     INITIAL_WIDTH, INITIAL_HEIGHT,     /* win_open() fills these in later */
  113.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,    /* win_open() fills these in later */
  114.     no_margins,
  115.     dci_black_and_white,
  116.     0,        /* not open yet */
  117.     2,        /* nColors */
  118.     win_ddb_copy_to_clipboard,
  119.     win_ddb_repaint,
  120.     win_ddb_alloc_bitmap,
  121.     win_ddb_free_bitmap
  122. };
  123.  
  124. /* Open the win_ddb driver */
  125. private int
  126. win_ddb_open(gx_device *dev)
  127. {    int code = win_open(dev);
  128.     HDC hdc;
  129.     if ( code < 0 ) return code;
  130.  
  131.     /* Create the backing bitmap. */
  132.     code = win_ddb_alloc_bitmap((gx_device_win *)dev, dev);
  133.     if ( code < 0 ) return code;
  134.  
  135.     /* Create the bitmap and DC for copy_mono. */
  136.     hdc = GetDC(wdev->hwndimg);
  137.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  138.     wdev->hdcmono = CreateCompatibleDC(hdc);
  139.     if ( wdev->hbmmono == NULL || wdev->hdcmono == NULL )
  140.     {    win_ddb_free_bitmap((gx_device_win *)dev);
  141.         ReleaseDC(wdev->hwndimg, hdc);
  142.         return win_nomemory();
  143.     }
  144.     SetMapMode(wdev->hdcmono, GetMapMode(hdc));
  145.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  146.     wdev->bm_id = gx_no_bitmap_id;
  147.     ReleaseDC(wdev->hwndimg, hdc);
  148.  
  149.     /* create palette and tools for bitmap */
  150.     if ((wdev->lpalette = win_makepalette((gx_device_win *)dev))
  151.         == (LPLOGPALETTE)NULL)
  152.         return win_nomemory();
  153.     wdev->hpalette = CreatePalette(wdev->lpalette);
  154.     (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  155.     RealizePalette(wdev->hdcbit);
  156.     win_maketools(wdev,wdev->hdcbit);
  157.  
  158.     wdev->hdctext = wdev->hdcbit;    /* draw text here */
  159.  
  160.     return 0;
  161. }
  162.  
  163. /* Close the win_ddb driver */
  164. private int
  165. win_ddb_close(gx_device *dev)
  166. {
  167.     /* Free resources */
  168.  
  169.     win_destroytools(wdev);
  170.     DeleteDC(wdev->hdcmono);
  171.     win_ddb_free_bitmap((gx_device_win *)dev);
  172.     DeleteObject(wdev->hpalette);
  173.     DeleteObject(wdev->hbmmono);
  174.     gs_free((char *)(wdev->lpalette), 1, sizeof(LOGPALETTE) + 
  175.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  176.         "win_ddb_close");
  177.  
  178.     return win_close(dev);
  179. }
  180.  
  181. /* Map a r-g-b color to the colors available under Windows */
  182. private gx_color_index
  183. win_ddb_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  184.   gx_color_value b)
  185. {    int i = wdev->nColors;
  186.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  187.     LPLOGPALETTE lipal = wdev->limgpalette;
  188.     LPLOGPALETTE lpal = wdev->lpalette;
  189.     if ( color != i ) return color;
  190.  
  191.     /* We just added a color to the window palette. */
  192.     /* Add it to the bitmap palette as well. */
  193.  
  194.     DeleteObject(wdev->hpalette);
  195.     lpal->palPalEntry[i].peFlags = NULL;
  196.     lpal->palPalEntry[i].peRed   =  lipal->palPalEntry[i].peRed;
  197.     lpal->palPalEntry[i].peGreen =  lipal->palPalEntry[i].peGreen;
  198.     lpal->palPalEntry[i].peBlue  =  lipal->palPalEntry[i].peBlue;
  199.     lpal->palNumEntries = i+1;
  200.     wdev->hpalette = CreatePalette(lpal);
  201.     (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  202.     RealizePalette(wdev->hdcbit);
  203.     win_addtool(wdev, i);
  204.  
  205.     return color;
  206. }
  207.  
  208. /* Macro for filling a rectangle with a color. */
  209. /* Note that it starts with a declaration. */
  210. #define fill_rect(x, y, w, h, color)\
  211. RECT rect;\
  212. rect.left = x, rect.top = y;\
  213. rect.right = x + w, rect.bottom = y + h;\
  214. FillRect(wdev->hdcbit, &rect, wdev->hbrushs[(int)color])
  215.  
  216.  
  217. /* Fill a rectangle. */
  218. private int
  219. win_ddb_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  220.   gx_color_index color)
  221. {
  222.     fit_fill(dev, x, y, w, h);
  223.     /* Use PatBlt for filling.  Special-case black. */
  224.     if ( color == 0 )
  225.         PatBlt(wdev->hdcbit, x, y, w, h, rop_write_0s);
  226.     else
  227.     {    select_brush((int)color);
  228.         PatBlt(wdev->hdcbit, x, y, w, h, rop_write_pattern);
  229.     }
  230.     win_update((gx_device_win *)dev);
  231.  
  232.     return 0;
  233. }
  234.  
  235. /* Tile a rectangle.  If neither color is transparent, */
  236. /* pre-clear the rectangle to color0 and just tile with color1. */
  237. /* This is faster because of how win_copy_mono is implemented. */
  238. /* Note that this also does the right thing for colored tiles. */
  239. private int
  240. win_ddb_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  241.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  242.   int px, int py)
  243. {    fit_fill(dev, x, y, w, h);
  244.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  245.        {    fill_rect(x, y, w, h, czero);
  246.         czero = gx_no_color_index;
  247.        }
  248.     if ( tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  249.          (px | py) == 0 && cone != gx_no_color_index
  250.        )
  251.     {    /* We can do this much more efficie